package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.Category;
import com.design.softwaredesign.front.CategoryInfo;
import com.design.softwaredesign.mapper.BackCategoryMapper;
import com.design.softwaredesign.service.BackCategoryService;
import com.design.softwaredesign.vo.CategoryVo;
import com.design.softwaredesign.vo.DataVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/5 20:28
 */
@Service
public class BackCategoryServiceImpl implements BackCategoryService {

    @Autowired
    private BackCategoryMapper backCategoryMapper;

    @Override
    public DataVo getCategoryByState(Integer categoryState) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("获取类型失败");
        try {
            List<Category> categoryList = backCategoryMapper.selectCategory(categoryState);
            if(categoryState == 1) {
                List<CategoryVo> categoryVos = new ArrayList<>();
                for(Category category : categoryList) {
                    CategoryVo categoryVo = new CategoryVo();
                    categoryVo.setKey(category.getGoodCategoryId());
                    categoryVo.setLabel(category.getGoodCategory());
                    categoryVo.setCategoryState(category.getCategoryState());
                    categoryVos.add(categoryVo);
                }
                dataVo.setCode(200);
                dataVo.setMsg("获取类型成功");
                dataVo.setData(categoryVos);
            }
            else {
                List<Integer> disagreeList = new ArrayList<>();
                for(Category category : categoryList) {
                   disagreeList.add(category.getGoodCategoryId());
                }
                dataVo.setCode(200);
                dataVo.setMsg("获取类型成功");
                dataVo.setData(disagreeList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo updateCategoryState(CategoryInfo categoryInfo) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("更新类型状态失败");
        try {
            if(categoryInfo.getDirection().equals("left")) {
                for(int i = 0; i < categoryInfo.getCategoryIds().length; i++) {
                    backCategoryMapper.updateCategoryState(categoryInfo.getCategoryIds()[i], 1);
                }
            }
            if(categoryInfo.getDirection().equals("right")) {
                for(int i = 0; i < categoryInfo.getCategoryIds().length; i++) {
                    backCategoryMapper.updateCategoryState(categoryInfo.getCategoryIds()[i], 2);
                }
            }
            dataVo.setCode(200);
            dataVo.setMsg("更新类型状态成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }
}
